package com.wk.swagger;

import static springfox.documentation.swagger.schema.ApiModelProperties.allowableValueFromString;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.google.common.base.Optional;
import com.google.common.collect.Lists;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.OperationBuilderPlugin;
import springfox.documentation.spi.service.contexts.OperationContext;
import springfox.documentation.spring.web.DescriptionResolver;
import springfox.documentation.swagger.common.SwaggerPluginSupport;

@Component
@Order(SwaggerPluginSupport.SWAGGER_PLUGIN_ORDER+1)
public class ApiNotesExtReader implements OperationBuilderPlugin {
	private final DescriptionResolver descriptions;

	@Autowired
	public ApiNotesExtReader(DescriptionResolver descriptions) {
		this.descriptions = descriptions;
	}

	@Override
	public void apply(OperationContext context) {
		Optional<ApiOperation> methodAnnotation = context.findAnnotation(ApiOperation.class);
		String notes = "";
		if (methodAnnotation.isPresent() && StringUtils.hasText(methodAnnotation.get().notes())) {
			notes = descriptions.resolve(methodAnnotation.get().notes());
		}
		List<Parameter> parameters=readParameters(context);
		if(parameters!=null&&parameters.size()>0) {
			StringBuffer sb=new StringBuffer();
			sb.append("<p> <p/><div><a style=\"font-size:1.1em;\">参数描述</a><p> </p></div>");
			sb.append("<table class=\"fullwidth parameters\">");
			sb.append(createHeader());
			for (Parameter parameter : parameters) {
				sb.append(createRow(parameter));
			}
			sb.append("</table>");
			notes = notes +sb;
		}
		if(StringUtils.hasText(notes)) {
			context.operationBuilder().notes(notes);
		}
	}

	private List<Parameter> readParameters(OperationContext context) {
		Optional<ApiNotesExt> annotation = context.findAnnotation(ApiNotesExt.class);

		List<Parameter> parameters = Lists.newArrayList();
		if (annotation.isPresent()) {
			for (ApiImplicitParam param : annotation.get().value()) {
				parameters.add(implicitParameter( param));
			}
		}

		return parameters;
	}
	public String createHeader() {
		StringBuffer sb=new StringBuffer();
		sb.append("<thead>");
		sb.append("<tr>");
			sb.append("<th>");
			sb.append("名称");
			sb.append("</th>");
			sb.append("<th>");
			sb.append("是否必填");
			sb.append("</th>");
			sb.append("<th>");
			sb.append("描述");
			sb.append("</th>");
		sb.append("</tr>");
		sb.append("</thead>");
		return sb.toString();
	}
	public String createRow(Parameter p) {
		StringBuffer sb=new StringBuffer();
		sb.append("<tr>");
			sb.append("<td>");
			sb.append(p.getName());
			sb.append("</td>");
			sb.append("<td>");
			sb.append(p.isRequired());
			sb.append("</td>");
			sb.append("<td>");
			sb.append(p.getDescription());
			sb.append("</td>");
		sb.append("</tr>");
		return sb.toString();
	}
	 Parameter implicitParameter( ApiImplicitParam param) {
	    return new ParameterBuilder()
	        .name(param.name())
	        .description(descriptions.resolve(param.value()))
	        .defaultValue(param.defaultValue())
	        .required(param.required())
	        .allowMultiple(param.allowMultiple())
	        .allowableValues(allowableValueFromString(param.allowableValues()))
	        .parameterType(param.paramType())
	        .parameterAccess(param.access())
	        .build();
	  }
	@Override
	public boolean supports(DocumentationType delimiter) {
		return SwaggerPluginSupport.pluginDoesApply(delimiter);
	}
}
